<template>
	<view class="auth">
		<view class="text">
			<view>为了向您提供更好的用户体验</view>
			<view>乐附近会在特定场景下向您申请以下手机系统权限</view>
		</view>

		<view class="card">
			<view class="list flex_r_between" v-for="(item,index) in auth" :key="index"
				@click="toAuthSetting(item)">
				<view class="left">
					<text>{{item.title}}</text>
					<view>{{item.content}}</view>
				</view>
				<view class="right">
					<text style="color: #88898A;" v-if="item.result == 1">已开启</text>
					<text style="color: #0091FF;" v-else>去设置</text>
					<uni-icons type="forward" :size="20"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	export default {
		data() {
			return {
				auth: [
					{
						name:'CAMERA',
						title:'相机权限',
						content:'实现您扫描、拍照、录视频',
						key:'android.permission.CAMERA',
						os:'camera',
						result:0,
					},
					{
						name:'READ_EXTERNAL_STORAGE',
						title:'读取权限',
						content:'访问您的相册，实现上传头像、认证等功能',
						key:'android.permission.READ_EXTERNAL_STORAGE',
						os:'photoLibrary',
						result:0,
					},
					{
						name:'ACCESS_FINE_LOCATION',
						title:'位置权限',
						content:'根据您位置信息为您推荐周边的生活服务',
						key:'android.permission.ACCESS_FINE_LOCATION',
						os:'location',
						result:0,
					},
					{
						name:'CALL_PHONE',
						title:'拨打电话权限',
						content:'快捷拨打电话',
						key:'android.permission.CALL_PHONE',
						os:'contact',
						result:0,
					},
					{
						name:'RECORD_AUDIO',
						title:'麦克风权限',
						content:'用于录制有声视频或使用直播服务',
						key:'android.permission.RECORD_AUDIO',
						os:'record',
						result:0,
					},
				]
			}
		},
		methods: {
			toAuthSetting(e , flag = true) {
				if(uni.getStorageSync('osName') == 'ios'){
					let auth = permision.judgeIosPermission(e.os)
					if(auth){
						item.result = 1
					}else {
						if(flag){
							uni.openAppAuthorizeSetting()
						}
					}
					// permision.judgeIosPermission(e.os).then(res=>{
					// 	this.auth.forEach(item=>{
					// 		if(item.name == e.name){
					// 			item.result = res
					// 		}
					// 	})
					// 	if(flag){
					// 		if (res == -1) {
					// 			uni.openAppAuthorizeSetting()
					// 		}
					// 	}
					// })
				}else {
					this.$util.authorize(e.key).then(res => {
						this.auth.forEach(item=>{
							if(item.name == e.name){
								item.result = res
							}
						})
						if(flag){
							if (res == -1) {
								uni.openAppAuthorizeSetting()
							}
						}
					})
				}
				
			},
		},
		onLoad() {
			let auth = uni.getAppAuthorizeSetting()
			this.auth[0].result = auth.cameraAuthorized == 'authorized' ? 1 : 0
			// this.auth[1].result = auth.albumAuthorized == 'authorized' ? 1 : 0
			this.auth[2].result = auth.locationAuthorized == 'authorized' ? 1 : 0
			this.auth[4].result = auth.microphoneAuthorized == 'authorized' ? 1 : 0
			this.toAuthSetting(this.auth[3],false)
			this.toAuthSetting(this.auth[1],false)
		}
	}
</script>

<style lang="scss" scoped>
	.auth {
		background: #F5F7F8;
		min-height: 100vh;
	}

	.text {
		padding: 30rpx 30rpx 0rpx;

		view {
			font-size: 24rpx;
			color: #9C9C9C;
			line-height: 33rpx;
		}
	}

	.card {
		padding: 10rpx 30rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		margin-top: 20rpx;

		.list {
			padding: 20rpx 0;

			.left {
				text {
					font-size: 30rpx;
					color: #333333;
					line-height: 42rpx;
				}

				view {
					font-size: 24rpx;
					color: #88898A;
					line-height: 33rpx;
					margin-top: 10rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				text {
					font-size: 26rpx;
					line-height: 37rpx;
					margin-right: 6rpx;
				}
			}
		}
	}
</style>